package com.cuitrip.component;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;

/**
 * Created by MiJack on 2015/12/13.
 */
public class CheckLayout extends LinearLayout implements Checkable {
    View contentView;
    ViewGroup container;
    IconTextView iconTextView;
    private boolean checked;

    public CheckLayout(Context context) {
        this(context, null);
    }

    public CheckLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View root = LayoutInflater.from(context).inflate(R.layout.base_checked_layout, this, true);
        container = (ViewGroup) root.findViewById(R.id.container);
        iconTextView = (IconTextView) root.findViewById(R.id.icon);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CheckLayout, defStyleAttr, R.style.CheckLayout_Default);
        setOrientation(HORIZONTAL);
        final int N = a.getIndexCount();
        for (int i = 0; i < N; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CheckLayout_contentLayout:
                    int layoutId = a.getResourceId(i, android.R.layout.simple_list_item_1);
                    contentView = LayoutInflater.from(context).inflate(layoutId, container, false);
                    setContentView(contentView);
                    break;
            }
        }
        a.recycle();
    }

    public void setContentView(View view) {
        this.contentView = view;
        container.removeAllViews();
        container.addView(contentView);
    }

    public View getContentView() {
        return contentView;
    }

    public void setChecked(boolean checked) {
        if (this.checked == checked) {
            return;
        }
        this.checked = checked;
        iconTextView.setTextColor(getResources().getColor(checked ? R.color.apple_7ecc1e : R.color.ganshi_ded8d7));
    }

    public boolean isChecked() {
        return checked;
    }

    public void toggle() {
        setChecked(!checked);
    }
}
